package com.cby.android.library.base

import android.os.Bundle
import android.view.View

/**
 * @description: 懒加载
 * @author: ChenBaoYang
 * @createDate: 2022/5/9 3:57 下午
 */
abstract class BaseLazyFragment : BaseFragment() {

    /**
     * 是否对用户可见
     */
    private var isVisibleToUser = false

    /**
     * 第一次初始化view
     */
    private var isInitView = false


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        isInitView = true
        super.onViewCreated(view, savedInstanceState)
        lazyLoad()
    }

    /**
     * 懒加载
     */
    private fun lazyLoad() {
        if (isInitView && isVisibleToUser) {
            loadData()
            isVisibleToUser = false
            isInitView = false
        }
    }

    abstract fun loadData()

    override fun onResume() {
        super.onResume()
        isVisibleToUser = true
        lazyLoad()
    }

    override fun onPause() {
        super.onPause()
        isVisibleToUser = false
    }
}